package mobi.greenplate.client.connect;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;

/**
 *
 * @author dmakariev
 */
abstract class DefaultTemplate implements HttpClientTemplate {

    public abstract int execute(HttpClient httpClient) throws Exception;

    DefaultTemplate(HttpClientTemplateContext context) {
        this.context = context;
    }
    private final HttpClientTemplateContext context;

    protected final HttpClientTemplateContext getContext() {
        return context;
    }

    protected static int handleResponse(final HttpResponse response, final ResponseHandler responseHandler) throws Exception {
        final HttpEntity entity = response.getEntity();
        final int statusCode = response.getStatusLine().getStatusCode();
        responseHandler.setHttpResponse(response);
        if (entity != null) {
            responseHandler.handleContent(entity.getContent(), entity.getContentLength());
        }
        return statusCode;
    }
}
